<?php
/********************************************************************************
 * Achievement template tags
 */
class DPA_Achievement_Template {
	var $current_achievement = -1;
	var $achievement_count;
	var $achievements;
	var $achievement;

	var $in_the_loop;

	function dpa_achievement_template( $user_id, $type, $category, $limit ) {
		global $bp;

		if ( !$user_id )
			$user_id = $bp->displayed_user->id;

		switch ( $type ) {
			case 'recent':
				$this->achievements = dpa_get_recent_achievements( $user_id, $limit, $category );
				break;

			case 'unlocked':
				$this->achievements = dpa_get_all_unlocked_achievements( $user_id, $limit, $category );
				break;

			case 'enabled':
				$this->achievements = dpa_get_all_enabled_achievements( $limit, $category );
				break;

			case 'disabled':
			$this->achievements = dpa_get_all_disabled_achievements( $limit, $category );
				break;

			case 'all': default:
				$this->achievements = dpa_get_all_achievements( $limit, $category );
				break;
		}

		$this->achievement_count = count( $this->achievements );
	}

	function has_achievements() {
		if ( $this->achievement_count )
			return true;
		
		return false;
	}

	function next_achievement() {
		$this->current_achievement++;
		$this->achievement = $this->achievements[$this->current_achievement];
		
		return $this->achievement;
	}
	
	function rewind_achievements() {
		$this->current_achievement = -1;
		if ( $this->achievement_count > 0 ) {
			$this->achievement = $this->achievements[0];
		}
	}
	
	function user_achievements() { 
		if ( $this->current_achievement + 1 < $this->achievement_count ) {
			return true;
		} elseif ( $this->current_achievement + 1 == $this->achievement_count ) {
			do_action( 'loop_end' );
			// Do some cleaning up after the loop
			$this->rewind_achievements();
		}

		$this->in_the_loop = false;
		return false;
	}

	function the_achievement() {
		global $achievement, $bp;

		$this->in_the_loop = true;
		$this->achievement = $this->next_achievement();

		if ( 0 == $this->current_achievement ) // loop has just started
			do_action( 'loop_start' );
	}
}

function dpa_has_achievements( $args = '' ) {
	global $bp, $achievements_template;

	$defaults = array(
		'category' => '',
		'limit' => 0,
		'type' => 'all',
		'user_id' => 0
	);

	$r = wp_parse_args( $args, $defaults );
	extract( $r, EXTR_SKIP );

	$achievements_template = new DPA_Achievement_Template( $user_id, $type, $category, $limit );
	return $achievements_template->has_achievements();
}

function dpa_user_achievements() {
	global $achievements_template;
	return $achievements_template->user_achievements();
}

function dpa_the_achievement() {
	global $achievements_template;
	return $achievements_template->the_achievement();
}

function dpa_achievement_id() {
	echo dpa_get_achievement_id();
}
	function dpa_get_achievement_id() {
		global $achievements_template;

		return apply_filters( 'dpa_get_achievement_id', $achievements_template->achievement->id ) + 0;
	}

function dpa_achievement_name() {
	echo dpa_get_achievement_name();
}
	function dpa_get_achievement_name() {
		global $achievements_template;

		return apply_filters( 'dpa_get_achievement_name', $achievements_template->achievement->nice_name );
	}

function dpa_achievement_description() {
	echo dpa_get_achievement_description();
}
	function dpa_get_achievement_description() {
		global $achievements_template;

		return apply_filters( 'dpa_get_achievement_description', $achievements_template->achievement->description );
	}

function dpa_achievement_points() {
	echo dpa_get_achievement_points();
}
	function dpa_get_achievement_points() {
		global $achievements_template;

		return apply_filters( 'dpa_get_achievement_points', $achievements_template->achievement->points );
	}

function dpa_achievement_shortname() {
	echo dpa_get_achievement_shortname();
}
	function dpa_get_achievement_shortname() {
		global $achievements_template;

		return apply_filters( 'dpa_get_achievement_shortname', $achievements_template->achievement->short_name );
	}

function dpa_achievement_is_active() {
	echo dpa_get_achievement_is_active();
}
	function dpa_get_achievement_is_active() {
		global $achievements_template;

		return apply_filters( 'dpa_get_achievement_is_active', $achievements_template->achievement->active );
	}

function dpa_achievement_counter() {
	echo dpa_get_achievement_counter();
}
	function dpa_get_achievement_counter() {
		global $achievements_template, $bp;

		return apply_filters( 'dpa_get_achievement_counter', get_usermeta( $bp->displayed_user->id, 'dpa_' . dpa_get_achievement_shortname() . '_counter' ) ) + 0;
	}

function dpa_achievement_is_special() {
	echo dpa_get_achievement_is_special();
}
	function dpa_get_achievement_is_special() {
		global $achievements_template;

		return apply_filters( 'dpa_get_achievement_is_special', $achievements_template->achievement->is_special );
	}

function dpa_achievement_category() {
	echo dpa_get_achievement_category();
}
	function dpa_get_achievement_category() {
		global $achievements_template;

		return apply_filters( 'dpa_get_achievement_category', $achievements_template->achievement->category );
	}

function dpa_achievement_unlocked_date() {
	echo dpa_get_achievement_unlocked_date();
}
	function dpa_get_achievement_unlocked_date() {
		global $achievements_template;

		if ( is_null( $achievements_template->achievement->achieved_at ) )
			return '';

		return apply_filters( 'dpa_get_achievement_unlocked_date', mysql2date( get_option( 'date_format' ), $achievements_template->achievement->achieved_at ) );
	}

function dpa_is_achievement_unlocked() {
	if ( dpa_get_is_achievement_unlocked() )
		return 'unlocked';
	else
		return 'locked';
}
	function dpa_get_is_achievement_unlocked() {
		global $achievements_template;

		return ( !is_null( $achievements_template->achievement->achieved_at ) );
	}


/********************************************************************************
 * Category template tags
 */
class DPA_Category_Template {
	var $current_category = -1;
	var $category_count;
	var $categories;
	var $category;

	var $in_the_loop;

	function dpa_category_template( $user_id, $type, $category ) {
		global $bp;

		if ( !$user_id )
			$user_id = $bp->displayed_user->id;

		switch ( $type ) {
			case 'overall':
				$this->categories = dpa_get_overall_category( $user_id );	
				break;

			case 'active': default:
				$this->categories = dpa_get_active_categories( $user_id, $category );	
				break;
		}

		$this->category_count = count( $this->categories );
	}

	function has_categories() {
		if ( $this->category_count )
			return true;
		
		return false;
	}

	function next_category() {
		$this->current_category++;
		$this->category = $this->categories[$this->current_category];
		
		return $this->category;
	}
	
	function rewind_categories() {
		$this->current_category = -1;
		if ( $this->category_count > 0 ) {
			$this->category = $this->categories[0];
		}
	}
	
	function achievement_categories() { 
		if ( $this->current_category + 1 < $this->category_count ) {
			return true;
		} elseif ( $this->current_category + 1 == $this->category_count ) {
			do_action( 'loop_end' );
			// Do some cleaning up after the loop
			$this->rewind_categories();
		}

		$this->in_the_loop = false;
		return false;
	}

	function the_category() {
		global $category, $bp;

		$this->in_the_loop = true;
		$this->category = $this->next_category();

		if ( 0 == $this->current_category ) // loop has just started
			do_action( 'loop_start' );
	}
}

function dpa_has_categories( $args = '' ) {
	global $bp, $categories_template;

	$defaults = array(
		'category' => '',
		'type' => 'active',
		'user_id' => 0
	);

	$r = wp_parse_args( $args, $defaults );
	extract( $r, EXTR_SKIP );

	$categories_template = new DPA_Category_Template( $user_id, $type, $category );
	return $categories_template->has_categories();
}

function dpa_achievement_categories() {
	global $categories_template;
	return $categories_template->achievement_categories();
}

function dpa_the_category() {
	global $categories_template;
	return $categories_template->the_category();
}

function dpa_category_id() {
	echo dpa_get_category_id();
}
	function dpa_get_category_id() {
		global $categories_template;

		return apply_filters( 'dpa_get_category_id', $categories_template->category->id ) + 0;
	}

function dpa_category_percentage() {
	echo dpa_get_category_percentage();
}
	function dpa_get_category_percentage() {
		global $categories_template;

		return apply_filters( 'dpa_get_category_percentage', $categories_template->category->percentage );
	}

function dpa_category_name() {
	echo dpa_get_category_name();
}
	function dpa_get_category_name() {
		global $categories_template;

		return apply_filters( 'dpa_get_achievement_category', $categories_template->category->name );
	}

function dpa_category_number_unlocked() {
	echo dpa_get_category_number_unlocked();
}
	function dpa_get_category_number_unlocked() {
		global $categories_template;

		return apply_filters( 'dpa_get_category_number_unlocked', $categories_template->category->unlocked_count );
	}

function dpa_category_number_total() {
	echo dpa_get_category_number_total();
}
	function dpa_get_category_number_total() {
		global $categories_template;

		return apply_filters( 'dpa_get_category_number_total', $categories_template->category->total_count );
	}

function dpa_category_slug() {
	echo dpa_get_category_slug();
}
	function dpa_get_category_slug() {
		global $categories_template;

		return apply_filters( 'dpa_make_category_slug', dpa_get_category_name() );
	}


/********************************************************************************
 * General template tags
 */
function dpa_header_tabs() {
	global $bp, $wpdb;

	if ( $bp->loggedin_user->id != $bp->displayed_user->id ) {

		$categories = wp_cache_get( 'dpa_active_categories', 'dpa' );
		if ( !$categories ) {
			$categories = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT category FROM {$bp->achievements->table_achievements} as a, {$bp->achievements->table_categories} as c WHERE a.active = 1 AND c.id = a.category_id" ) );
			wp_cache_set( 'dpa_active_categories', $categories, 'dpa' );
		}

		$categories[] = __( 'Summary', 'dpa' );
		$current_action = apply_filters( 'dpa_make_category_slug', bp_current_action() );

		foreach ( (array) $categories as $category_name ) {
			$slug = apply_filters( 'dpa_make_category_slug', $category_name );
			$achievements_link = $bp->displayed_user->domain . $bp->achievements->slug . '/' . $slug;
			$class = '';

			if ( $slug == $current_action )
				$class = 'current';
			?>
			<li class="<?php echo $class ?>"><a href="<?php echo esc_attr( $achievements_link ) ?>"><?php echo apply_filters( 'dpa_filter_achievement_category', $category_name ); ?></a></li>
<?php
		}
	}

	do_action( 'dpa_header_tabs' );
}
?>